package com.example.administrator.School;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import com.example.administrator.School.adpter.HorizontalListView;
import com.example.administrator.School.adpter.HorizontalListViewAdapter;
import com.example.administrator.School.common.result.Result;
import com.example.administrator.School.until.BaseDetailActivity;
import com.example.administrator.School.until.FaceDialog;
import com.example.administrator.School.until.GlideImageLoader;
import com.example.administrator.School.until.RecyAdapter;
import com.google.gson.reflect.TypeToken;
import com.makeramen.roundedimageview.RoundedImageView;
import com.youth.banner.Banner;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


public class MainActivity extends BaseDetailActivity<Map> implements RecyAdapter.OnItemClickListener {

    @Override
    protected int getContentView() {
        return R.layout.activity_main_index;
    }
    //值日的横向列表
    @BindView(R.id.horizon_listview)
    HorizontalListView hListView;
    HorizontalListViewAdapter hListViewAdapter;

    //课程表的列表
    @BindView(R.id.main_list_course)
    ListView listview;

    //滚动图
    @BindView(R.id.banner)
    Banner banner;
    List<String> images=new ArrayList<>();
    //textSwitcher
    public int curStr;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            next(null);
        }
    };
    String[] string = new String[]{
            "高数",
            "概率论",
            "网络"
    };
    @BindView(R.id.textSwitcher)
    TextSwitcher textSwitcher;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.main_teacher_avatar)
    RoundedImageView img;
    private Integer[] mImgIds = {R.drawable.tx, R.drawable.biaoyang, R.drawable.zhiri, R.drawable.setting};
    private List<Integer> datas;
    private RecyAdapter recyAdapter;
    private Handler mHandler=new Handler();
    private LinearLayoutManager layoutManager;

    private int oldItem=0;


    @Override
    protected boolean initBundle(Bundle bundle) {
        return true;
    }

    //页面传值回调
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void initWidget() {
        super.initWidget();
        //初始化横向列表
        String[] titles = {"怀师", "南怀瑾军校", "闭关", "南怀瑾", "南公庄严照", "怀师法相"};
        final int[] ids = {R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher};
        hListViewAdapter = new HorizontalListViewAdapter(this,titles,ids);
        hListView.setAdapter(hListViewAdapter);

        //初始化课程表的listview
        List<String> listdata = new ArrayList<String>();
        listdata.add("1.语文 7.30-8.10");
        listdata.add("2.语文 7.30-8.10");
        listdata.add("3.语文 7.30-8.10");
        listdata.add("4.语文 7.30-8.10");
        listdata.add("5.语文 7.30-8.10");
        listdata.add("6.语文 7.30-8.10");
        listdata.add("7.语文 7.30-8.10");
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_item,listdata);//listdata和str均可
        listview.setAdapter(arrayAdapter);



        //设置图片加载器
        images.add("http://pic33.nipic.com/20131007/13639685_123501617185_2.jpg");
        images.add("http://pic33.nipic.com/20131007/13639685_123501617185_2.jpg");
        images.add("http://pic33.nipic.com/20131007/13639685_123501617185_2.jpg");  //图片路径
        banner.setImageLoader(new GlideImageLoader());
        banner.setDelayTime(3000);//滚动时间
        //设置图片集合
        banner.setImages(images);
        //banner设置方法全部调用完毕时最后调用
        banner.start();

        //text切换
        textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                TextView textView = new TextView(YuanbaoApplication.getContextObject());
                textView.setTextSize(24);
                textView.setTextColor(Color.BLACK);
                return textView;
            }
        });
        new Thread(){
            @Override
            public void run() {
                while (true){
                    Message message = handler.obtainMessage();
                    message.obj = 0;
                    handler.sendMessage(message);
                    try {
                        sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
    private void next(View scource){
        if(curStr<string.length){
            curStr = curStr+1;
            textSwitcher.setText(string[curStr-1]);
        }else{
            curStr =0;
            textSwitcher.setText(string[curStr]);
        }
        // textSwitcher.setText(string[curStr = ( curStr++ % string.length )]);
    }

    @Override
    protected void initData() {
        super.initData();
        initDatas();
        initRecy();
        img.setImageResource(datas.get(0));
        recyAdapter.setOnItemClickListener(this);
    }

    @Override
    protected Type getType() {
        return new TypeToken<Result<Map>>() {
        }.getType();
    }

    @Override
    protected void setData(Result<Map> result) {

    }

    Runnable scrollRunnable=new Runnable() {
        @Override
        public void run() {
            recyclerview.scrollBy(50,0);
            //获取最后一个可见view的位置
            int lastItemPosition = layoutManager.findLastVisibleItemPosition();
            //获取第一个可见view的位置
            int firstItemPosition = layoutManager.findFirstVisibleItemPosition();
            System.out.println(lastItemPosition + "   " + firstItemPosition);
            int firstItem=layoutManager.findFirstVisibleItemPosition();
            if(firstItem!=oldItem&&firstItem>0){
                oldItem=firstItem;
                img.setImageResource(datas.get(oldItem%datas.size()));
            }

            Log.e("aaaaaaaaa", "run: firstItem:"+firstItem );


            mHandler.postDelayed(scrollRunnable,3000);
        }
    };

    private void initRecy() {
        recyAdapter=new RecyAdapter(this,datas);

        layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerview.setLayoutManager(layoutManager);
        recyclerview.setAdapter(recyAdapter);
    }

    private void initDatas() {
        datas=new ArrayList<>();
        for (int i = 0; i <mImgIds.length ; i++) {
            datas.add(mImgIds[i]);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mHandler.postDelayed(scrollRunnable,2000);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mHandler.removeCallbacks(scrollRunnable);
    }

    @Override
    public void onItemClick(View view, int tag) {
        Toast.makeText(this,"第"+tag+"张图片被点击了",Toast.LENGTH_SHORT).show();
    }


    @OnClick(R.id.main_text_class_name)
    void yingyong(){
        startActivity(new Intent(this,DetailActivity.class));
    }

    @OnClick(R.id.zhiri)
    void zhiri(){
        startActivity(new Intent(this,DutyActivity.class));
    }

    @OnClick(R.id.main_course_profile)
    void kaoqin(){
        startActivity(new Intent(this,KaoQinActivity.class));
    }

    @OnClick(R.id.main_layout_course)
    void kc(){
        startActivity(new Intent(this,KcbActivity.class));
    }
    @OnClick(R.id.photo)
    void photo(){
        startActivity(new Intent(this,PhotoActivity.class));
    }

    @OnClick(R.id.main_teacher_title)
    void huodong(){
        startActivity(new Intent(this,HuoDongActivity.class));
    }
    @OnClick(R.id.tongzhi)
    void tongzhi(){
        startActivity(new Intent(this,TongZhiActivity.class));
    }

    @OnClick(R.id.imageView2)
    void yingyong1(){
        startActivity(new Intent(this,YingyongActivity.class));
    }

    @OnClick(R.id.face)
    void face(){
        FaceDialog.Builder builder = new FaceDialog.Builder(this);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                //设置你的操作事项
            }
        });
        builder.setNegativeButton("取消",
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.create().show();
    }
    //滚动图
    @OnClick(R.id.banner)
    void banner(){
        startActivity(new Intent(this,PhotoActivity.class));
    }


//
//    @OnClick(R.id.logo)
//    void detail(){
//        //跳转到详情页面
////        startActivity(new Intent(this,DetailActivity.class));
////        startActivity(new Intent(this,DutyActivity.class));
////        startActivity(new Intent(this,KaoQinActivity.class));
////        startActivity(new Intent(this,KcbActivity.class));
//
////        CustomDialog.Builder builder = new CustomDialog.Builder(this);
//////        builder.setMessage("这个就是自定义的提示框");
//////        builder.setTitle("提示");
////        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
////            public void onClick(DialogInterface dialog, int which) {
////                dialog.dismiss();
////                //设置你的操作事项
////            }
////        });
////
////        builder.setNegativeButton("取消",
////                new android.content.DialogInterface.OnClickListener() {
////                    public void onClick(DialogInterface dialog, int which) {
////                        dialog.dismiss();
////                    }
////                });
////
////        builder.create().show();
//
////        startActivity(new Intent(this,TongZhiActivity.class));
////        startActivity(new Intent(this,PhotoActivity.class));
////        startActivity(new Intent(this,YingyongActivity.class));
//
////        startActivity(new Intent(this,StudentActivity.class));
//        startActivity(new Intent(this,HuoDongActivity.class));
//    }

}

